package geometry;

import general.Case;
import general.Mechanisme;

import java.util.ArrayList;

import javax.media.opengl.GL;

public class PlanetVar
{
	public ArrayList<ArrayList<Zone>> carte = new ArrayList<ArrayList<Zone>>();
	
	int nbNiveau = 1;

	Mechanisme	m;
	
	public void construireCarte( int taille, Mechanisme m )
	{
		this.m = m;
		
		//int pas = taille + 1;
		
		for( int i = 0 ; i < nbNiveau ; i++ )
		{
			ArrayList<Zone> plateau = creationZonesA( 1 , taille/2 );
			
			carte.add( plateau );
			
			//pas = pas/2;
		}
		
		//afficherPlateau(0);
	}
	
	/*
	public ArrayList<Zone> creationZonesB( int pas , int lim )
	{	
		
		ArrayList<Zone> plateau = new ArrayList<Zone>();
		
		int passage = lim/2;
		
		PointSI p1,p2,p3,p4;
		
		//System.out.println("First exa : " + (lim-pas*1) + " moins de " + (lim-pas) );
		int l;

		double modifR = 0;
		
		System.out.println("Pas : " + pas + " fin " + lim );
		
		int bordureInf = 0;
		int bordureSup = lim-pas;
		int bordureLim = lim;
		
		int ii,jj,kk;
		
		for( int i = 0 ; i < lim ; i += pas )
		{
			
			for( int j = 0 ; j < lim ; j += pas )
			{
				
				for( int k = 0 ; k < lim ; k += pas )
				{
					
					if( i == bordureSup && j < bordureLim && k < bordureLim )
					{
						l = 0;
						
						ii = i - lim/2-pas;
						jj = j - lim/2-pas;
						kk = k - lim/2-pas;
						
						p1 = pointSI( ii , jj , kk , passage);
						
						p2 = pointSI( ii , jj+pas , kk , passage);
						
						p3 = pointSI( ii , jj+pas , kk+pas , passage);
						
						p4 = pointSI( ii , jj , kk+pas , passage);
						
						System.out.println( i + " " + j);
						
						m.cases[l][j][k].p = p1;
						m.cases[l][j+pas][k].p = p2;
						m.cases[l][j+pas][k+pas].p = p3;
						m.cases[l][j][k+pas].p = p4;
						
						Zone z = new Zone( 1 );
						z.setB( j , j + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][j][k] , m.g[l][j+pas][k] , m.g[l][j+pas][k+pas] , m.g[l][j][k+pas] );
						plateau.add( z );
					}
					
					if( j == bordureSup && i < bordureLim && k < bordureLim )
					{
						l = 1;
						
						p1 = pointSI( i , j, k , passage);
						
						p2 = pointSI( i+pas , j , k , passage);
						
						p3 = pointSI( i+pas , j , k+pas , passage);
						
						p4 = pointSI( i , j , k+pas , passage);
						
						m.cases[l][i][k].p = p1;
						m.cases[l][i+pas][k].p = p2;
						m.cases[l][i+pas][k+pas].p = p3;
						m.cases[l][i][k+pas].p = p4;
						
						Zone z = new Zone( 2 );
						z.setB( i , i + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i][k] , m.g[l][i+pas][k] , m.g[l][i+pas][k+pas] , m.g[l][i][k+pas] );
						plateau.add( z );
					}
					
					if( k == bordureSup && i < bordureLim && j < bordureLim )
					{
						l = 2;
						
						p1 = pointSI( i , j , k , passage);
						
						p2 = pointSI( i+pas , j , k  , passage);
						
						p3 = pointSI( i+pas , j+pas , k  , passage);
						
						p4 = pointSI( i , j+pas , k  , passage);
						
						m.cases[l][i][j].p = p1;
						m.cases[l][i+pas][j].p = p2;
						m.cases[l][i+pas][j+pas].p = p3;
						m.cases[l][i][j+pas].p = p4;
						
						Zone z = new Zone( 3 );
						z.setB( i , i + pas , j , j + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i][j] , m.g[l][i+pas][j] , m.g[l][i+pas][j+pas] , m.g[l][i][j+pas] );
						plateau.add( z );
					}
					
					if( i == bordureInf && j < bordureLim && k < bordureLim  )
					{
						l = 3;
						
						p1 = pointSI( i  , j , k , passage);
						
						p2 = pointSI( i  , j+pas , k , passage);
						
						p3 = pointSI( i  , j+pas , k+pas , passage);
						
						p4 = pointSI( i  , j , k+pas , passage);
						
						m.cases[l][j][k].p = p1;
						m.cases[l][j+pas][k].p = p2;
						m.cases[l][j+pas][k+pas].p = p3;
						m.cases[l][j][k+pas].p = p4;
						
						Zone z = new Zone( 4 );
						z.setB( j , j + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						
						//System.out.println((j) + " " + (k) );
						
						z.setA( m.g[l][j][k] , m.g[l][j+pas][k] , m.g[l][j+pas][k+pas] , m.g[l][j][k+pas] );
						plateau.add( z );
					}
					
					if( j == bordureInf  && k < bordureLim && i < bordureLim )
					{
						l = 4;
						
						p1 = pointSI( i , j  , k , passage);
						
						p2 = pointSI( i+pas , j  , k , passage);
						
						p3 = pointSI( i+pas , j  , k+pas , passage);
						
						p4 = pointSI( i , j  , k+pas , passage);
						
						m.cases[l][i][k].p = p1;
						m.cases[l][i+pas][k].p = p2;
						m.cases[l][i+pas][k+pas].p = p3;
						m.cases[l][i][k+pas].p = p4;
						
						Zone z = new Zone( 5 );
						z.setB( i , i + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i][k] , m.g[l][i+pas][k] , m.g[l][i+pas][k+pas] , m.g[l][i][k+pas] );
						plateau.add( z );
						
					}
					
					if( k == bordureInf && i < bordureLim && j < bordureLim )
					{
						l = 5;
						
						p1 = pointSI( i , j , k  , passage);
						
						p2 = pointSI( i+pas , j , k  , passage);
						
						p3 = pointSI( i+pas , j+pas , k  , passage);
						
						p4 = pointSI( i , j+pas , k , passage);
						
						m.cases[l][i][j].p = p1;
						m.cases[l][i+pas][j].p = p2;
						m.cases[l][i+pas][j+pas].p = p3;
						m.cases[l][i][j+pas].p = p4;
						
						Zone z = new Zone( 6 );
						z.setB( i , i + pas , j , j + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i][j] , m.g[l][i+pas][j] , m.g[l][i+pas][j+pas] , m.g[l][i][j+pas] );
						plateau.add( z );
					}
				}
			}
		}
		
		System.out.println("Face 1 : " + Zone.tab[0] );
		System.out.println("Face 2 : " + Zone.tab[1] );
		System.out.println("Face 3 : " + Zone.tab[2] );
		System.out.println("Face 4 : " + Zone.tab[3] );
		System.out.println("Face 5 : " + Zone.tab[4] );
		System.out.println("Face 6 : " + Zone.tab[5] );
		
		
		return plateau;
	}*/
	
	public ArrayList<Zone> creationZonesA( int pas , int lim )
	{	
		
		//System.out.println("Pas : " + pas + " deb : " + (-lim+pas) + " fin " + lim );
		ArrayList<Zone> plateau = new ArrayList<Zone>();
		
		double passage = (double)lim;
		
		PointSI p1,p2,p3,p4;
		
		//System.out.println("First exa : " + (lim-pas*1) + " moins de " + (lim-pas) );
		int l;
		
		int min = -lim+pas-1;
		
		double modifR = 0;
		
		System.out.println("Taille : " + (lim-min+1) + " g : " + m.g[0].length );
		
		for( int i = -lim ; i <= lim ; i += pas )
		{
			
			for( int j = -lim ; j <= lim ; j += pas )
			{
				
				for( int k = -lim ; k <= lim ; k += pas )
				{
					
					if( i == lim && j < lim && k < lim )
					{
						l = 0;
						
						p1 = pointSI( i + m.g[l][j-min][k-min] * modifR , j , k , passage);
						
						p2 = pointSI( i + m.g[l][j+pas-min][k-min] * modifR , j+pas , k , passage);
						
						p3 = pointSI( i + m.g[l][j+pas-min][k+pas-min] * modifR , j+pas , k+pas , passage);
						
						p4 = pointSI( i + m.g[l][j-min][k+pas-min] * modifR , j , k+pas , passage);
						
						
						
						Zone z = new Zone( 1 );
						z.setB( j , j + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][j-min][k-min] , m.g[l][j+pas-min][k-min] , m.g[l][j+pas-min][k+pas-min] , m.g[l][j-min][k+pas-min] );
						plateau.add( z );
						
						m.cases[l][j-min][k-min].zone = z;
					}
					
					if( j == lim && i < lim && k < lim )
					{
						l = 1;
						
						p1 = pointSI( i , j + m.g[l][i-min][k-min] * modifR , k , passage);
						
						p2 = pointSI( i+pas , j + m.g[l][i+pas-min][k-min] * modifR , k , passage);
						
						p3 = pointSI( i+pas , j + m.g[l][i+pas-min][k+pas-min] * modifR , k+pas , passage);
						
						p4 = pointSI( i , j + m.g[l][i-min][k+pas-min] * modifR , k+pas , passage);
						
						
						
						Zone z = new Zone( 2 );
						z.setB( i , i + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i-min][k-min] , m.g[l][i+pas-min][k-min] , m.g[l][i+pas-min][k+pas-min] , m.g[l][i-min][k+pas-min] );
						plateau.add( z );
						
						m.cases[l][i-min][k-min].zone = z;
					}
					
					if( k == lim && i < lim && j < lim )
					{
						l = 2;
						
						p1 = pointSI( i , j , k + m.g[l][i-min][j-min] * modifR , passage);
						
						p2 = pointSI( i+pas , j , k + m.g[l][i+pas-min][j-min] * modifR , passage);
						
						p3 = pointSI( i+pas , j+pas , k + m.g[l][i+pas-min][j+pas-min] * modifR , passage);
						
						p4 = pointSI( i , j+pas , k + m.g[l][i-min][j+pas-min] * modifR , passage);
						
						Zone z = new Zone( 3 );
						z.setB( i , i + pas , j , j + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i-min][j-min] , m.g[l][i+pas-min][j-min] , m.g[l][i+pas-min][j+pas-min] , m.g[l][i-min][j+pas-min] );
						plateau.add( z );
						
						m.cases[l][i-min][j-min].zone = z;
					}
					
					if( i == -lim && j < lim && k < lim  )
					{
						l = 3;
						
						p1 = pointSI( i + m.g[l][j-min][k-min] * modifR , j , k , passage);
						
						p2 = pointSI( i + m.g[l][j+pas-min][k-min] * modifR , j+pas , k , passage);
						
						p3 = pointSI( i + m.g[l][j+pas-min][k+pas-min] * modifR , j+pas , k+pas , passage);
						
						p4 = pointSI( i + m.g[l][j-min][k+pas-min] * modifR , j , k+pas , passage);
						
						Zone z = new Zone( 4 );
						z.setB( j , j + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						
						//System.out.println((j-min) + " " + (k-min) );
						
						z.setA( m.g[l][j-min][k-min] , m.g[l][j+pas-min][k-min] , m.g[l][j+pas-min][k+pas-min] , m.g[l][j-min][k+pas-min] );
						plateau.add( z );
						
						m.cases[l][j-min][k-min].zone = z;
					}
					
					if( j == -lim  && k < lim && i < lim )
					{
						l = 4;
						
						p1 = pointSI( i , j + m.g[l][i-min][k-min] * modifR , k , passage);
						
						p2 = pointSI( i+pas , j + m.g[l][i+pas-min][k-min] * modifR , k , passage);
						
						p3 = pointSI( i+pas , j + m.g[l][i+pas-min][k+pas-min] * modifR , k+pas , passage);
						
						p4 = pointSI( i , j + m.g[l][i-min][k+pas-min] * modifR , k+pas , passage);

						Zone z = new Zone( 5 );
						z.setB( i , i + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i-min][k-min] , m.g[l][i+pas-min][k-min] , m.g[l][i+pas-min][k+pas-min] , m.g[l][i-min][k+pas-min] );
						plateau.add( z );
						
						m.cases[l][i-min][k-min].zone = z;
					}
					
					if( k == -lim && i < lim && j < lim )
					{
						l = 5;
						
						p1 = pointSI( i , j , k + m.g[l][i-min][j-min] * modifR , passage);
						
						p2 = pointSI( i+pas , j , k + m.g[l][i+pas-min][j-min] * modifR , passage);
						
						p3 = pointSI( i+pas , j+pas , k + m.g[l][i+pas-min][j+pas-min] * modifR , passage);
						
						p4 = pointSI( i , j+pas , k + m.g[l][i-min][j+pas-min] * modifR , passage);

						Zone z = new Zone( 6 );
						z.setB( i , i + pas , j , j + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( m.g[l][i-min][j-min] , m.g[l][i+pas-min][j-min] , m.g[l][i+pas-min][j+pas-min] , m.g[l][i-min][j+pas-min] );
						plateau.add( z );
						
						m.cases[l][i-min][j-min].zone = z;
					}
				}
			}
		}
		
		System.out.println("Face 1 : " + Zone.tab[0] );
		System.out.println("Face 2 : " + Zone.tab[1] );
		System.out.println("Face 3 : " + Zone.tab[2] );
		System.out.println("Face 4 : " + Zone.tab[3] );
		System.out.println("Face 5 : " + Zone.tab[4] );
		System.out.println("Face 6 : " + Zone.tab[5] );
		
		return plateau;
	}
	
	private PointSI pointSI( double x , double y , double z , double passage)
	{
		x = x /passage;
		y = y /passage;
		z = z /passage;
		
		double sx = x * Math.sqrt(1.0f - y * y * 0.5f - z * z * 0.5f + y * y * z * z / 3.0f);
		 
		double sy = y * Math.sqrt(1.0f - z * z * 0.5f - x * x * 0.5f + z * z * x * x / 3.0f);
		 
		double sz = z * Math.sqrt(1.0f - x * x * 0.5f - y * y * 0.5f + x * x * y * y / 3.0f);
		
		return new PointSI(sx, sy, sz);
	}
	
	public void afficherPlateau( int p )
	{
		for( int i = 0 ; i < carte.get(p).size() ; i++ )
		{
			if( carte.get(p).get(i).face == 1 )
			{
				System.out.println( i + " " + carte.get(p).get(i) + "\n" );
			}
		}
	}
	
	/*
	public static void main(String[] args)
	{
		PlanetVar pl = new PlanetVar();
		
		pl.construireCarte( 9 , null );
		
		//pl.afficherPlateau(0);
		

	}
	*/
	
	public ArrayList<Zone> creationZonesB( float[][] g, int pas , int lim )
	{	
		
		//System.out.println("Pas : " + pas + " deb : " + (-lim+pas) + " fin " + lim );
		ArrayList<Zone> plateau = new ArrayList<Zone>();
		
		double passage = (double)lim;
		
		PointSI p1,p2,p3,p4;
		
		//System.out.println("First exa : " + (lim-pas*1) + " moins de " + (lim-pas) );
		int l;
		
		int min = -lim+pas-1;
		
		double modifR = 0;
		
		System.out.println("Taille : " + (lim-min+1) + " g : " + g.length );
		
		for( int i = -lim ; i <= lim ; i += pas )
		{
			
			for( int j = -lim ; j <= lim ; j += pas )
			{
				
				for( int k = -lim ; k <= lim ; k += pas )
				{
					
					if( i == lim && j < lim && k < lim )
					{
						l = 0;
						
						p1 = pointSI( i + g[j-min][k-min] * modifR , j , k , passage);
						
						p2 = pointSI( i + g[j+pas-min][k-min] * modifR , j+pas , k , passage);
						
						p3 = pointSI( i + g[j+pas-min][k+pas-min] * modifR , j+pas , k+pas , passage);
						
						p4 = pointSI( i + g[j-min][k+pas-min] * modifR , j , k+pas , passage);

						Zone z = new Zone( 1 );
						z.setB( j , j + pas , k , k + pas );
						z.setP( p1 , p2 , p3 , p4 );
						z.setA( g[j-min][k-min] , g[j+pas-min][k-min] , g[j+pas-min][k+pas-min] , g[j-min][k+pas-min] );
						plateau.add( z );
					}
				}
			}
		}
		
		System.out.println("Face 1 : " + Zone.tab[0] );
		
		return plateau;
	}
}
